<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="部门名称"
          :labelCol="{span: 4}"
          :wrapperCol="{span: 20}"
        >
          <a-input v-decorator="['name', {rules: [{required: true, message: '请输入部门名称!'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>


<script>
import pick from 'lodash.pick'
  import {
    postAxiosUrl,
    getUrl
  } from '@/api/manage'
export default {
  data () {
    return {
      id : '',
      checked : false,
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
    }
  },
  created () {
  },
  methods: {
  add () {
    this.id = ''
    this.form.resetFields()
    this.visible = true
    this.checked = false
    this.checkedKeys = []
   
  },
  edit (record) {
     const { form: { setFieldsValue } } = this
     this.visible = true
     const me = this
      this.$nextTick(() => {
      me.id = record.id
      setFieldsValue(pick(record, ['name']))
      setFieldsValue(pick(record, ['description']))
       if(record.type == '1'){
         me.checked = true
         this.checkedKeys = [0]
       }else{
         me.checked = false
         me.fetchPermission()
       }
    })
    
  },
  close () {
      this.$emit('close')
      this.visible = false
  },
  handleCancel () {
      this.close()
  },
  onChange(e) {
      this.checked = e.target.checked
      if(this.checked == true)
        this.checkedKeys = [0]
      else
         this.checkedKeys = []
  },
  handleOk () {
      const _this = this
      
      // 触发表单验证
      this.form.validateFields((err, values) => {
        // 验证表单没错误
        if (!err) {
          console.log('form values', values)
          _this.confirmLoading = true
         values.id = this.id
         postAxiosUrl('sysDeparment/save', values)
           .then(data => {
              if(!data.success){
                 _this.$message.warning(data.error)
                 _this.confirmLoading = false
               }else{
                 _this.visible = false
                 _this.confirmLoading = false
                 _this.$emit('ok', values)
               }           
             })
        }
      })
    }
  }
}
</script>

<style>
</style>
